package org.jctc.els.modules.biz.controller.message;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.jctc.els.modules.biz.service.message.IMessageService;
import org.jctc.els.modules.biz.entity.message.Message;
import org.jctc.els.modules.biz.entity.message.MessageExpand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 消息表 前端控制器
 * </p>
 *
 * @author lyh
 * @since 2021-02-26
 */
@Api(tags = {"消息表"})
@RestController
@AllArgsConstructor
@RequestMapping("/message")
public class MessageController {

    private IMessageService messageService;


    @ApiOperation(value = "新增消息表")
    @PostMapping()
    public R save(@RequestBody MessageExpand messageExpand) {
        return R.status(messageService.save(messageExpand));
    }

    @ApiOperation(value = "删除消息表")
    @DeleteMapping("{id}")
    public R remove(@PathVariable("id") Long id) {
        return R.status(messageService.remove(id));
    }

    @ApiOperation(value = "更新消息表")
    @PutMapping()
    public R edit(@RequestBody MessageExpand messageExpand) {
        return R.status(messageService.edit(messageExpand));
    }

    @ApiOperation(value = "查询消息表分页数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数")
    })
    @GetMapping()
    public R<Page<MessageExpand>> selectListByPage(@RequestParam(value = "pageNum") Integer pageNum,
                                                @RequestParam(value = "pageSize") Integer pageSize,
                                                @RequestParam(value = "title", required = false) String title,
                                                @RequestParam(value = "messageTypeCode") String messageTypeCode) {
		 Long schoolId = 0L;
		Page<MessageExpand> messageExpandPage = messageService.selectListByPage(pageNum, pageSize, title, messageTypeCode, schoolId);
		return R.data(messageExpandPage);
    }

    @ApiOperation(value = "根据id查询消息表")
    @GetMapping("{id}")
    public R<MessageExpand> selectById(@PathVariable("id") Long id) {
		MessageExpand messageExpand = messageService.selectById(id);
		return R.data(messageExpand);
    }


    @ApiOperation(value = "发布消息表")
    @PutMapping("/release")
    public R releaseMessage(@RequestBody MessageExpand messageExpand) {
        boolean res = false;
        res = messageService.releaseMessage(messageExpand);
        return R.status(res);
    }

    @GetMapping("/all_read")
    public R allRead(@RequestParam(value = "userId") Long userId) {
        boolean res = false;
        res = messageService.allRead(userId);
        return R.status(res);
    }

    @DeleteMapping()
    public R deleteMessage(@RequestParam(value = "id") Long id,
                          @RequestParam(value = "userId") Long userId) {
        boolean res = false;
        res = messageService.deleteMessage(id, userId);
        return R.status(res);
    }

    @GetMapping("/user")
    public R<Page<MessageExpand>> selectMessageByUserId(@RequestParam(value = "pageNum") Integer pageNum,
                                              @RequestParam(value = "pageSize") Integer pageSize,
                                              @RequestParam(value = "title", required = false) String title,
                                              @RequestParam(value = "messageTypeCode") String messageTypeCode,
                                              @RequestParam(value = "userId") Long userId) {
        Page<MessageExpand> messageExpandPage = messageService.selectMessageByUserId(pageNum, pageSize, title, messageTypeCode, userId);
        return R.data(messageExpandPage);
    }

    @PostMapping("/mess")
    public R saveMessage(@RequestBody Message message){
        Boolean b = messageService.saveMessage(message);
        return R.status(b);
    }

}
